package cool.mtc.minghe.data.model.sys.user.form;

import cool.mtc.minghe.data.entity.sys.user.UserDO;
import cool.mtc.minghe.data.entity.sys.user.UserOrgRoleDO;
import cool.mtc.web.model.form.LongIdFormSupport;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 明河
 */
@Schema(description = "表单")
@Data
public class UserForm implements LongIdFormSupport {

    @Schema(description = "ID")
    private Long userId;

    @Schema(description = "用户昵称", required = true)
    @NotBlank(message = "user.nick_name.required")
    private String nickName;

    @Schema(description = "手机号码")
    private String mobile;

    @Schema(description = "邮箱")
    private String email;

    @Schema(description = "用户状态")
    @NotNull(message = "user.status.required")
    private Long statusDictId;

    @Schema(description = "用户单位")
    @NotEmpty(message = "user.org.required")
    private List<UserOrgForm> userOrgList = new ArrayList<>();

    @Override
    public Long getId() {
        return userId;
    }

    public UserDO transToDO() {
        UserDO userDO = new UserDO();
        this.updateDO(userDO);
        return userDO;
    }

    public void updateDO(UserDO userDO) {
        BeanUtils.copyProperties(this, userDO);
    }

    public List<UserOrgRoleDO> transToUserOrgRoleDO(Long userId) {
        List<UserOrgRoleDO> userOrgRoleDOList = new ArrayList<>();
        userOrgList.forEach(userOrg -> {
            userOrgRoleDOList.addAll(userOrg.transToUserOrgRoleDO(userId));
        });
        return userOrgRoleDOList;
    }
}
